package com.ruoyi.place.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.google.common.collect.Lists;
import com.ruoyi.common.core.service.TcCommonService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.place.domain.bo.PlaceInfoBo;
import com.ruoyi.place.domain.vo.PlaceInfoVo;
import com.ruoyi.place.domain.PlaceInfo;
import com.ruoyi.place.mapper.PlaceInfoMapper;
import com.ruoyi.place.service.IPlaceInfoService;

import java.util.List;
import java.util.Collection;

/**
 * 房屋信息Service业务层处理
 *
 * @author strongme
 * @date 2023-05-02
 */
@RequiredArgsConstructor
@Service
public class PlaceInfoServiceImpl implements IPlaceInfoService {

    private final PlaceInfoMapper baseMapper;
    private final TcCommonService tcCommonService;

    /**
     * 查询房屋信息
     */
    @Override
    public PlaceInfoVo queryById(Long id){
        PlaceInfoVo vo  = baseMapper.selectVoById(id);
        List<PlaceInfoVo> results = Lists.newArrayList(vo);
        tcCommonService.renderTcInfo(results);
        return vo;
    }

    /**
     * 查询房屋信息列表
     */
    @Override
    public TableDataInfo<PlaceInfoVo> queryPageList(PlaceInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PlaceInfo> lqw = buildQueryWrapper(bo);
        Page<PlaceInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        tcCommonService.renderTcInfo(result.getRecords());
        return TableDataInfo.build(result);
    }

    /**
     * 查询房屋信息列表
     */
    @Override
    public List<PlaceInfoVo> queryList(PlaceInfoBo bo) {
        LambdaQueryWrapper<PlaceInfo> lqw = buildQueryWrapper(bo);
        List<PlaceInfoVo> result = baseMapper.selectVoList(lqw);
        tcCommonService.renderTcInfo(result);
        return result;
    }

    private LambdaQueryWrapper<PlaceInfo> buildQueryWrapper(PlaceInfoBo bo) {
        LambdaQueryWrapper<PlaceInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPlaceType() != null, PlaceInfo::getPlaceType, bo.getPlaceType());
        lqw.eq(StringUtils.isNotBlank(bo.getArea()), PlaceInfo::getArea, bo.getArea());
        lqw.like(StringUtils.isNotBlank(bo.getName()), PlaceInfo::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), PlaceInfo::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), PlaceInfo::getAddress, bo.getAddress());
        return lqw;
    }

    /**
     * 新增房屋信息
     */
    @Override
    public Boolean insertByBo(PlaceInfoBo bo) {
        PlaceInfo add = BeanUtil.toBean(bo, PlaceInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改房屋信息
     */
    @Override
    public Boolean updateByBo(PlaceInfoBo bo) {
        PlaceInfo update = BeanUtil.toBean(bo, PlaceInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PlaceInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除房屋信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
